package threadcoreknowledge.createthread.threadobjectclasscommommethods;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 每一秒输出当前时间，被中断，被观察
 * Thread.sleep
 * TimeUnit.SECONDS.sleep()
 */
public class SleepInterrupted implements Runnable{

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new SleepInterrupted());
        thread.start();
        Thread.sleep(3000);
        thread.interrupt();
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(new Date());

            try {
                //推荐使用这种sleep
                TimeUnit.HOURS.sleep(1);
                TimeUnit.MINUTES.sleep(3);
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("我被中断了");
            }



        }
    }
}
